<template>
	<view class="viewBg">
		<view class="lineH2"></view>

		<view class="img-content">
			<image style="width:300upx; height:300upx;  border-radius:100%; overflow:hidden;" :src="userProtrait" @tap="chooseImg"></image>

			<view class="update-content">修改头像</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userProtrait: '../../../static/img/default_protrait.png'
			};
		},
		onLoad() {
			if (getApp().globalData.photo.length > 0) {
				this.userProtrait = 'http://image.tripln.top/gridfile' + getApp().globalData.photo
			}
			console.log('头像'+this.userProtrait);
		},
		methods: {
			chooseImg() { //选择图片
				let _this = this;
				let _contentResult;
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						var imageSrc = res.tempFilePaths[0]
						uni.uploadFile({
							url: 'http://grid.tripln.top/api/xagrid/upload',
							filePath: imageSrc,
							fileType: 'image',
							name: 'data',
							success: (res) => {
								_contentResult = JSON.parse(res.data); //转义字符 得到结果集
								this.$http.post('ac/xagrid/appUserService/updateUser', { //更新个人信息
									photo: _contentResult.data[0].path
								}).then((res) => {
									if (res.errcode === '0') { //0成功  1失败
										if (_contentResult.data.length > 0) {
											this.userProtrait = 'http://image.tripln.top/gridfile' + _contentResult.data[0].path;
										}
										uni.showToast({
											title: '上传成功',
											icon: 'success',
											duration: 1000
										})
										getApp().globalData.photo = _contentResult.data[0].path;
										this.setTime();
									} else {
										uni.showToast({
											title: '上传失败',
											icon: 'none',
											duration: 1000
										})
									}
								});
							},
							fail: (err) => {
								console.log('uploadImage fail', err);
								uni.showModal({
									content: err.errMsg,
									showCancel: false
								});
							}
						});
					},
					fail: (err) => {
						console.log('chooseImage fail', err)
					}
				})
			},
			setTime() {
				setTimeout(function() {
					uni.navigateBack(); //数据回显
				}, 1000 * 2);
			},
		}
	}
</script>

<style>
	.lineH2 {
		height: 32upx;
		width: 100%;
		background-color: #F7F7F7;
	}

	.viewBg {
		width: 100%;
		height: 100%;

		/* background-color: #0FAEFF; */
	}

	.img-content {
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 64upx;
	}

	.update-content {
		color: #007AFF;
		margin-top: 16upx;
	}
</style>
